package com.baiduspring.dubbo.service.server.user;

import com.baiduspring.dao.entity.FollowAuthor;
import com.baiduspring.dao.entity.User;
import com.baiduspring.dao.mapper.FollowAuthorMapper;
import com.baiduspring.error.BaiduSpringException;
import com.baiduspring.service.user.FollowAuthorService;
import com.baiduspring.utils.PreconditionsUtils;
import com.baiduspring.vo.FollowAuthorVO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class FollowAuthorServiceImpl implements FollowAuthorService {
    @Resource
    private FollowAuthorMapper followAuthorMapper;

    @Override
    public Optional<Map<String, List<FollowAuthorVO>>> getAllByAccount(Long account) {
        FollowAuthor followAuthor = new FollowAuthor();
        followAuthor.setFollower(account);

        List<User> followAuthors = followAuthorMapper.selectUserBySelective(followAuthor);
        Map<String, List<FollowAuthorVO>> results = Maps.newHashMap();

        if (!CollectionUtils.isEmpty(followAuthors)) {
            int pageSize = 3;
            this.action(followAuthors, datas -> {
                int pages = datas.size() % pageSize == 0 ? datas.size() / pageSize : datas.size() / pageSize + 1;

                for (int i = 0; i < pages; i++) {
                    List<FollowAuthorVO> vos = Lists.newArrayList();
                    for (int j = 0; j < pageSize; j++) {
                        // 计算当前集合的索引值
                        int index = i * pageSize + j;
                        if (index < datas.size()) {
                            User fa = datas.get(index);
                            vos.add(new FollowAuthorVO().setAccount(fa.getAccount()).setPhoto(fa.getPhoto()));
                        }
                    }
                    results.put(UUID.randomUUID().toString(), vos);
                }
            });
        }

        return Optional.ofNullable(results);
    }

    @Override
    public Integer countMyFollew(Long account) {
        FollowAuthor followAuthor = new FollowAuthor();
        followAuthor.setFollower(account);
        return followAuthorMapper.countBySelective(followAuthor);
    }

    @Override
    public Integer countMyFans(Long account) {
        FollowAuthor followAuthor = new FollowAuthor();
        followAuthor.setAccount(account);
        return followAuthorMapper.countBySelective(followAuthor);
    }

    @Override
    public Integer addFollow(Long follower, Long account) {
        PreconditionsUtils.checkArgument(Objects.nonNull(account), "被关注人账号不能为空");
        FollowAuthor followAuthor = new FollowAuthor();
        followAuthor.setFollower(follower);
        followAuthor.setAccount(account);
        followAuthor.setCreateDate(System.currentTimeMillis());

        int count = followAuthorMapper.countBySelective(followAuthor);
        if (count > 0) {
            throw new BaiduSpringException("您已经关注了该作者");
        }

        int rows = followAuthorMapper.insertSelective(followAuthor);
        if (rows == 0) {
            log.error("添加关注异常：{}");
        }

        return rows;
    }

    private void action(List<User> followAuthors, CallBack callBack) {
        callBack.action(followAuthors);
    }

    private interface CallBack {
        void action(List<User> followAuthors);
    }
}
